package com.renwei.yunlong.activity.contacts;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.bean.FriendEnterpriseBean;
import com.renwei.yunlong.bean.SendInvaJsonBean;
import com.renwei.yunlong.event.MsgCompanyInviEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.CircleTextImage;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class CompanySendInvitationActivity extends BaseActivity implements HttpTaskListener {

    public static final String CODE = "code";
    public static final String NAME = "name";

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.iv_pic)
    CircleTextImage ivPic;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_work_number)
    TextView tvWorkNumber;
    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.tv_count)
    TextView tvCount;
    @Bind(R.id.tv_count_all)
    TextView tvCountAll;
    @Bind(R.id.bt_send)
    Button btSend;
    private String companyCode = "";
    private String companyName = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_invitation);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        companyCode = getIntent().getStringExtra(CODE) == null ? "" : getIntent().getStringExtra(CODE);
        companyName = getIntent().getStringExtra(NAME) == null ? "" : getIntent().getStringExtra(NAME);
        initView();
    }


    private void initView() {
        simpleTileView.setTitle("发起邀请");
        tvName.setText(companyName);
        ivPic.setText4CircleImage(String.valueOf(companyName));
        btSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //sendInvite();
                refreshCompanyInfo();
            }
        });
        etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = s.length();
                tvCount.setText("" + length);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }

    private void sendInvite() {
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(content)){
            content = getResources().getString(R.string.invitation_company_input);
        }

        HttpPresenter hp = new HttpPresenter(this);
        if ("1".equals(companyType)) {
            hp.setUrl(Api.$().OWNER_SEND_INVITATION);
            String json = new Gson().toJson(new SendInvaJsonBean(companyCode, ownerBean.getRows().getOwnerCode(), content,
                    ownerBean.getRows().getEmployeeId()));
            hp.putParam("jsonBean", json);
        } else if ("2".equals(companyType)) {
            hp.setUrl(Api.$().SERVICE_SEND_INVITATION);
            String json = new Gson().toJson(new SendInvaJsonBean(serviceLoginBean.getRows().getServiceProviderCode(), companyCode, content,
                    serviceLoginBean.getRows().getEmployeeId()));
            hp.putParam("jsonBean", json);
        }
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.setCallBack(this);
        hp.post();
    }

    public static void OpenActivity(Context context, String companyName, String companyCode) {
        Intent intent = new Intent(context, CompanySendInvitationActivity.class);
        intent.putExtra(NAME, companyName);
        intent.putExtra(CODE, companyCode);
        context.startActivity(intent);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                CommonBean bean = new Gson().fromJson(result, CommonBean.class);
                int code = bean.getMessage().getCode();
                if (code == 200) {
                    showCenterSuccessMsg("邀请发送成功");
                    finish();
                    EventBus.getDefault().post(new MsgCompanyInviEvent());
                } else if ("2".equals(companyType)) {//服务商
                    switch (code) {
                        case 1008:
                            showCenterInfoMsg("无权限访问");
                            break;
                        case 1005:
                            showCenterInfoMsg("此公司已绑定");
                            break;
                        case 1025:
                            showCenterInfoMsg("对方已发过申请，请进行确认！");
                            break;
                        case 1026:
                            showCenterInfoMsg("已发过申请，请等待对方确认！");
                            break;
                        default:
                            showCenterInfoMsg(bean.getMessage().getMessage());
                            break;
                    }
                } else if ("1".equals(companyType)) {//业主
                    switch (code) {
                        case 1008:
                            showCenterInfoMsg("无权限访问");
                            break;
                        case 1005:
                            showCenterInfoMsg("此公司已绑定");
                            break;
                        case 1025:
                            showCenterInfoMsg("已发过申请，请等待对方确认！");
                            break;
                        case 1026:
                            showCenterInfoMsg("对方已发过申请，请进行确认");
                            break;
                        default:
                            showCenterInfoMsg(bean.getMessage().getMessage());
                            break;
                    }
                }
                break;
            case 879:
                FriendEnterpriseBean enterpriseBean = new Gson().fromJson(result, FriendEnterpriseBean.class);
                AppHelper.setMyCompanyInfo(enterpriseBean);
                String flag = StringUtils.value(AppHelper.getMyCompanyInfo().getRows().getCheckFlag());
                if (!"2".equals(StringUtils.value(flag))) {
                    showCenterInfoMsg("贵公司尚未认证,无法添加企业好友");
                    return;
                } else {
                    sendInvite();
                }
                break;
            default:

                break;

        }
    }

    @Override
    public void onException(int requestId, String code) {
    }
}
